package com.ruoyi.heima.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.heima.domain.dto.QueryNewsPageDto;
import com.ruoyi.heima.domain.po.News;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.heima.service.INewsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 新闻管理Controller
 *
 * @author yangjianlin
 * @date 2025-02-10
 */
@RestController
@RequestMapping("/news")
public class NewsController extends BaseController {
    @Autowired
    private INewsService newsService;

    @GetMapping("/list")
    public TableDataInfo list(QueryNewsPageDto queryNewsPageDto) {
        startPage();
        List<News> list = newsService.selectNewsList(queryNewsPageDto);
        return getDataTable(list);
    }

    @RequiresPermissions("heima:news:add")
    @Log(title = "新闻管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news) {
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 根据id查询新闻管理
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id) {
        return success(newsService.selectNewsByNewsId(Long.valueOf(id)));

    }

    /**
     * 修改新闻管理
     */
    @RequiresPermissions("heima:news:edit")
    @Log(title = "新闻管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news) {
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 删除新闻管理
     */
    @RequiresPermissions("heima:news:remove")
    @Log(title = "新闻管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds) {
        return toAjax(newsService.deleteNewsByNewsIds(newsIds));
    }

    /**
     * 推送新闻
     */
    @PostMapping("/{newsIds}")
    public AjaxResult pushNews(@PathVariable List<Long> newsIds) {
        newsService.pushNews(newsIds);
        return AjaxResult.success("新闻发布成功");
    }

    /*
     * 根据通知公告id查询来自通知公告的新闻
     */
    @GetMapping("/notice/{noticeId}")
    public News selectNewsByNoticeId(@PathVariable("noticeId") Long noticeId) {
        return newsService.selectNewsByNoticeId(noticeId);
    }

    /*
     * 删除来自通知公告的新闻
     */
    @DeleteMapping("/notice/{noticeIds}")
    public Boolean deleteNewsFromNotice(@PathVariable("noticeIds") Long[] noticeIds) {
        return newsService.deleteNewsFromNotice(noticeIds);
    }

    /**
     * 远程接口调用方法
     * @param newsIds
     * @return
     */
//    @GetMapping
//    public List<NewsPojo> getNewsList(@RequestParam("newsIds")List<Long> newsIds){
//        return newsService.getNewsList(newsIds);
//    }

    /**
     * 导出全部新闻
     * @param response
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        List<News> list = newsService.selectAllNewList();
        ExcelUtil<News> util = new ExcelUtil<>(News.class);
        util.exportExcel(response, list, "全部新闻数据");
    }

    /**
     * 导出当前页新闻
     * @param response
     */
    @PostMapping("/exportCurrentPage")
    public void exportCurrentPage(HttpServletResponse response, QueryNewsPageDto dto) {
        startPage();
        List<News> list = newsService.selectNewsList(dto);
        ExcelUtil<News> util = new ExcelUtil<>(News.class);
        util.exportExcel(response, list, "当前页新闻数据");
    }

    /**
     * 删除一周前更新的已删除状态新闻
     * @return
     */
    @DeleteMapping("deleted")
    public AjaxResult deletedOneWeekAgoNews() {
        newsService.deletedOneWeekAgoNews();
        return AjaxResult.success("一周前更新的删除状态新闻已删除");
    }

    @ApiOperation("根据id批量查询新闻")
    @GetMapping
    public List<News> selectNewsByIds(@RequestParam("ids") List<Long> ids){
        return newsService.selectNewsByIdsCache(ids);
    }
}
